package com.sbl.bajw.pc.action;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.HashMap;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.sbl.bajw.common.web.JsonResultUtil;
import com.sbl.bajw.common.web.MessageException;
import com.sbl.bajw.common.web.RequestUnit;
import com.sbl.bajw.pc.model.TodoModel;
import com.sbl.bajw.pc.service.DictModelService;
import com.sbl.bajw.pc.service.TodoService;
import com.sbl.bajw.util.JsonUtil;

@Controller
@RequestMapping("/Todo")
public class TodoAction {
	
	@Resource
	private TodoService service;
	
	@Resource
	private DictModelService dictService;
	
	@RequestMapping(value = "/SendTodo")
	public ModelAndView SendTodo(HttpServletRequest request, HttpServletResponse response){
		SimpleDateFormat df = new SimpleDateFormat("yyyyMMdd");
		String id = service.getId();
		String dateStr = df.format(Calendar.getInstance().getTime());
		String todoCode = "4403" + dateStr + id;
		List<HashMap<String, Object>> todoTypes = dictService.getTodoType();
		List<HashMap<String, Object>> infoTypes = dictService.getInfoType();
		request.setAttribute("todoTypes", todoTypes);
		request.setAttribute("infoTypes", infoTypes);
		request.setAttribute("todoCode", todoCode);
		return new ModelAndView("/view/manager/todo/sendDialog");
	}
	
	@RequestMapping(value = "/main")
	public ModelAndView Main(HttpServletRequest request, HttpServletResponse response){
		SimpleDateFormat df = new SimpleDateFormat("yyyyMMdd");
		String id = service.getId();
		String dateStr = df.format(Calendar.getInstance().getTime());
		String todoCode = "4403" + dateStr + id;
		List<HashMap<String, Object>> todoTypes = dictService.getTodoType();
		List<HashMap<String, Object>> infoTypes = dictService.getInfoType();
		request.setAttribute("todoTypes", todoTypes);
		request.setAttribute("infoTypes", infoTypes);
		request.setAttribute("todoCode", todoCode);
		return new ModelAndView("/view/manager/todo/main");
	}
	
	@RequestMapping(value = "/add")
	public @ResponseBody String add() throws MessageException{
		TodoModel model = RequestUnit.getModel(TodoModel.class);
		service.Add(model);
		HashMap<String, String> result = new HashMap<String, String>();
		result.put("msg", "成功");
		result.put("success", "true");
		result.put("code", "1");
		String str = JsonUtil.ConvertObjectToJson(result);
		return str;
	}
	
	@RequestMapping(value = "/update")
	public @ResponseBody String update(){
		TodoModel model = RequestUnit.getModel(TodoModel.class);
		service.update(model);
		return JsonResultUtil.Success();
	}
	
	@RequestMapping(value = "/getDetailView")
	public ModelAndView getDetailView(HttpServletRequest request, HttpServletResponse response){
		String id = request.getParameter("todo_id");
		String userId = request.getParameter("user_id");
		HashMap<String, String> model = service.getModel(id,userId);
		request.setAttribute("model", model);
		return new ModelAndView("/view/manager/todo/detailDialog");
	}
	
	@RequestMapping(value = "/finish")
	public @ResponseBody String finish(HttpServletRequest request, HttpServletResponse response) throws Exception {
		service.finish();
		HashMap<String, String> result = new HashMap<String, String>();
		result.put("msg", "成功");
		result.put("success", "true");
		result.put("code", "1");
		String str = JsonUtil.ConvertObjectToJson(result);
		return str;
	}
	
	@RequestMapping(value = "/closeTodoByUser")
	public @ResponseBody String closeTodoByUser(HttpServletRequest request, HttpServletResponse response) throws Exception {
		String todoid = RequestUnit.getString("todo_id");
		String userid = RequestUnit.getString("user_id");
		service.closeTodoByUser(todoid, userid);
		return JsonResultUtil.Success();
	}
	
	@RequestMapping(value = "/getTodoCountByUser")
	public @ResponseBody String getTodoCountByUser(HttpServletRequest request, HttpServletResponse response) {
		String userId = RequestUnit.getString("user_id");
		HashMap<String, String> model = service.getTodoCountByUser(userId);
		String data = JsonUtil.ConvertObjectToJson(model);
		String result = JsonResultUtil.Success(data);
		return result;
	}
	
	@RequestMapping(value = "/getList")
	public @ResponseBody String getList(HttpServletRequest request, HttpServletResponse response){
		HashMap<String, Object> parm = new HashMap<String, Object>();
		String title = RequestUnit.getString("title");
		String todoUser = RequestUnit.getString("todoUser");
		String start = RequestUnit.getString("start");
		String stop = RequestUnit.getString("stop");
		String sendStart = RequestUnit.getString("sendStart");
		String sendStop = RequestUnit.getString("sendStop");
		String code = RequestUnit.getString("code");
		String sendUser = RequestUnit.getString("sendUser");
		String level = RequestUnit.getString("level");
		String todoType = RequestUnit.getString("todoType");
		String isfinish = RequestUnit.getString("isfinish");
		if(title!=null && !title.equals("")){
			parm.put("title", "%"+title+"%");
		}
		if(todoUser!=null && !todoUser.equals("")){
			parm.put("todoUser", "%"+todoUser+"%");
		}
		if(sendUser!=null && !sendUser.equals("")){
			parm.put("sendUser", "%"+sendUser+"%");
		}
		if(code!=null && !code.equals("")){
			parm.put("code", "%"+code+"%");
		}
		if(sendStart!=null && !sendStart.equals("")){
			parm.put("sendStart", sendStart);
		}
		if(sendStop!=null && !sendStop.equals("")){
			parm.put("sendStop", sendStop);
		}
		if(start!=null && !start.equals("")){
			parm.put("start", start);
		}
		if(stop!=null && !stop.equals("")){
			parm.put("stop", stop);
		}
		
		parm.put("todoType", todoType);
		parm.put("level", level);
		parm.put("isfinish", isfinish);
		String json = service.getList(parm);
		return json;
	}
	
	@RequestMapping(value = "/downList")
	public @ResponseBody String downList(HttpServletRequest request, HttpServletResponse response) throws Exception{
		HashMap<String, String> parm = new HashMap<String, String>();
		String title = RequestUnit.getString("title");
		String todoUser = RequestUnit.getString("todoUser");
		String start = RequestUnit.getString("start");
		String stop = RequestUnit.getString("stop");
		String sendStart = RequestUnit.getString("sendStart");
		String sendStop = RequestUnit.getString("sendStop");
		String code = RequestUnit.getString("code");
		String sendUser = RequestUnit.getString("sendUser");
		String level = RequestUnit.getString("level");
		String todoType = RequestUnit.getString("todoType");
		String isfinish = RequestUnit.getString("isfinish");
		if(title!=null && !title.equals("")){
			parm.put("title", "%"+title+"%");
		}
		if(todoUser!=null && !todoUser.equals("")){
			parm.put("todoUser", "%"+todoUser+"%");
		}
		if(sendUser!=null && !sendUser.equals("")){
			parm.put("sendUser", "%"+sendUser+"%");
		}
		if(code!=null && !code.equals("")){
			parm.put("code", "%"+code+"%");
		}
		if(sendStart!=null && !sendStart.equals("")){
			parm.put("sendStart", sendStart);
		}
		if(sendStop!=null && !sendStop.equals("")){
			parm.put("sendStop", sendStop);
		}
		if(start!=null && !start.equals("")){
			parm.put("start", start);
		}
		if(stop!=null && !stop.equals("")){
			parm.put("stop", stop);
		}
		
		parm.put("todoType", todoType);
		parm.put("level", level);
		parm.put("isfinish", isfinish);
		service.downList(parm);
		return null;
	}
	
	@RequestMapping(value = "/getListByUser")
	public @ResponseBody String getListByUser(HttpServletRequest request, HttpServletResponse response){
		String json = service.getListByUser();
		return JsonResultUtil.Success(json);
	}
	
	@RequestMapping(value = "/getDetail")
	public @ResponseBody String getDetail(HttpServletRequest request, HttpServletResponse response){
		String todo_id = RequestUnit.getString("todo_id");
		String user_id = RequestUnit.getString("user_id");
		HashMap<String, String> model = service.getModel(todo_id,user_id);
		service.setReaded(user_id,todo_id);
		String json = JsonUtil.ConvertObjectToJson(model);
		String result = JsonResultUtil.Success(json);
		return result;
	}
}
